#include <stdio.h>

int partition(const int a[], int start, int end, int index[])
{
    int i = start, j = end;
    int pivot = index[i];

    while (i < j) {
        while (a[index[j]] >= a[pivot] && i < j) j--;
        index[i] = index[j];

        while (a[index[i]] <= a[pivot] && i < j) i++;
        index[j] = index[i];
    }
    index[i] = pivot;

    return i;
}
void quicksort(const int a[], int start, int end, int index[])
{
    if (start < end) {
        int mid = partition(a, start, end, index);

        quicksort(a, start, mid - 1, index);
        quicksort(a, mid + 1, end, index);
    }
}

int main(int argc, char const *argv[])
{
    int len, n;
    scanf("%d%d", &len, &n);

    char strs[n][len + 1];
    int stats[n];
    int index[n];
    for (int i = 0; i < n; i++)
        scanf("%s", strs[i]);

    for (int i = 0; i < n; i++) {
        int ACGT[4] = {0};
        int j = 0;
        // initialize stats and index
        stats[i] = 0;
        index[i] = i;
        for (char *p = strs[i]; *p != '\0'; p++) {
            switch (*p) {
            case 'A':
                ACGT[0]++;
                j = 1; break;
            case 'C':
                ACGT[1]++;
                j = 2; break;
            case 'G':
                ACGT[2]++;
                j = 3; break;
            case 'T':
                ACGT[3]++;
                j = 4; break;
            default:
                break;
            }
            for (; j < 4; j++)
                stats[i] += ACGT[j];
        }
    }

    quicksort(stats, 0, n - 1, index);

    for (int i = 0; i < n; i++)
        printf("%s\n", strs[index[i]]);

    return 0;
}
